/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package samples;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.AttributeSet;
import javax.swing.text.Element;
import javax.swing.text.html.ListView;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.eclipse.swt.*;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.StackLayout;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ListViewExample {
	static DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	static DocumentBuilder builder;
	static final int ICON_WIDTH = 100;
	static final int ICON_HEIGHT = 100;
	
	static {
		try {
			builder = factory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();		
		}
	}    
	
	static Map<String, ImageIcon> imageMap = new HashMap<String, ImageIcon>();
	
    public static ImageIcon getAlbumArt(String artist, String album) {    		   
		try {
			String key = artist + "\t" + album;
			ImageIcon result = imageMap.get(key);
			if (result != null) return result;
			
			String url = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=" + URLEncoder.encode(artist, "UTF-8") + "&album=" + URLEncoder.encode(album, "UTF-8");
			Document doc = builder.parse(url);       		    
		    
		    XPath xpath = XPathFactory.newInstance().newXPath();
		    String imageUrl;
			imageUrl = (String) xpath.evaluate("//image[@size='medium']/text()", doc, XPathConstants.STRING);			
			result = new ImageIcon(new URL(imageUrl));
			result.setImage(result.getImage().getScaledInstance(ICON_WIDTH, ICON_HEIGHT, java.awt.Image.SCALE_SMOOTH));
			
			imageMap.put(key, result);
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return null;
		}	    	    
    }
    
    static String	listData[] =
	{
		"Houses of the holy",
		"Led Zeppelin",
		"Led Zeppelin II",
		"Led Zeppelin III",
		"Led Zeppelin IV",
		"Physical Graffiti",
		"Houses of the holy",
		"Presence",
		"The Song Remains The Same",
		"Coda"
	};
    
	public static void main(String [] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {																		
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setSize(600, 500);
		shell.setLayout(new GridLayout());
		Color selectedColor = new Color(display, 50, 92, 166);
		Color white = new Color(display, 255, 255, 255);
				
		Composite composite = new Composite(shell, SWT.H_SCROLL
		        | SWT.V_SCROLL);						
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		layout.makeColumnsEqualWidth = true;
		composite.setLayout(layout);						
	    
	    ImageData imgData = new Image(display, "c:\\temp.gif").getImageData().scaledTo(100, 100);
	    Image img = new Image(display, imgData);
	    
	    MouseAdapter doSelectAlbum = new MouseAdapter() {
	    	@Override
	    	public void mouseUp(MouseEvent arg0) {		    		
	    		System.out.println("Click");
	    	}
	    };
	    
	    for (int i = 0; i < 12; i++) {
		    Composite comp = new Composite(composite, SWT.NONE);
		    comp.setBackground(selectedColor);
		    comp.addMouseListener(doSelectAlbum);
		    					
			GridLayout innerLayout = new GridLayout();			
			comp.setLayout(innerLayout);		
	    		    			    	
			Label imageLabel = new Label(comp, SWT.NONE);
			imageLabel.setImage(img);
			imageLabel.addMouseListener(doSelectAlbum);
	    	
	    	Label label = new Label(comp, SWT.NONE);
	    	label.setBackground(selectedColor);
	    	label.setForeground(white);
	    	label.setText("The Corrs " + i);
	    	label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
	    	label.addMouseListener(doSelectAlbum);
	    }			    	    
	
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
		
	}
}